package io.zso.commons.mybatis.page;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.zso.commons.mybatis.entity.DataScope;
import io.zso.commons.tools.constant.Constant;

/**
 * 分页查询条件基础模型
 *
 * @author HP
 * @Description:
 * @date 2018-4-3 17:35
 */
public class PageQuery<T> extends Page<T> {

    /**
     * 传入参数：当前页码
     */
    protected Long page = 1L;
    /**
     * 传入参数：每页条数
     */
    protected Long limit = 40L;
    /**
     * 排序字段
     */
    protected String orderField;
    /**
     * 排序
     */
    protected String order;
    /**
     * 数据权限
     */
    protected DataScope sqlFilter;

    public PageQuery() {
        // 默认不分页
        this.setPageable(false);
    }

    public Long getPage() {
        return page;
    }

    public void setPage(Long page) {
        this.page = page;
        this.setCurrent(page);
        if (page != null && page > 0) {
            this.setSearchCount(true);
        }
    }

    public Long getLimit() {
        return limit;
    }

    public void setLimit(Long limit) {
        this.limit = limit;
        this.setSize(limit);
    }

    public String getOrderField() {
        return orderField;
    }

    public void setOrderField(String orderField) {
        this.orderField = orderField;

        //前端字段排序
        if (StringUtils.isNotEmpty(orderField) && StringUtils.isNotEmpty(order)) {
            if (Constant.ASC.equalsIgnoreCase(order)) {
                this.setAsc(orderField);
            } else {
                this.setDesc(orderField);
            }
        }
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;

        //前端字段排序
        if (StringUtils.isNotEmpty(orderField) && StringUtils.isNotEmpty(order)) {
            if (Constant.ASC.equalsIgnoreCase(order)) {
                this.setAsc(orderField);
            } else {
                this.setDesc(orderField);
            }
        }
    }

    public DataScope getSqlFilter() {
        return sqlFilter;
    }

    public void setSqlFilter(DataScope sqlFilter) {
        this.sqlFilter = sqlFilter;
    }

    /**
     * 用于控制是否分页
     *
     * @param pageable
     */
    public void setPageable(boolean pageable) {
        if (!pageable) {
            this.setSize(-1);
        } else {
            this.setSize(this.limit);
        }
    }

}
